package com.cherry.networksdk

import com.cherry.networksdk.response.DataResponse
import com.cherry.sdk.http.exception.ExceptionParser
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * @author 董棉生(dongmiansheng@parkingwang.com)
 * @since 18-11-29
 */

abstract class RxObserver<T : DataResponse<*>> : Observer<T> {
    override fun onComplete() {
    }

    override fun onSubscribe(d: Disposable) {
    }

    override fun onNext(t: T) {
        if (t.isSuccess()) {
            onSuccess(t.data)
        } else {
            //处理具体业务错误逻辑
        }
    }

    protected abstract fun onSuccess(data: Any?)

    override fun onError(e: Throwable) {
        e.printStackTrace()
        ExceptionParser.parse(e)
    }
}